<?php
namespace app\admin\model;

use think\Model;
use think\facade\Config;
class Brand extends Model
{   
	/*查询数据 */
	public function get_data(){
		$req=request()->param();
        if(!empty($req['brand_name'])||!empty($req['isindex'])){
        	if(!empty($req['brand_name'])){
        		$sear_where[]=['bname','like',"%".$req['brand_name']."%"];
        	}
        	if(!empty($req['isindex'])){
        		$sear_where[]=['isindex','=',$req['isindex']];
        	}
        	$list=Brand::where($sear_where)
			->where([['status','=',1]])
			->order('create_time','desc')
            ->paginate(Config::get('app.admin_page_number'),false,['query' => $req]);
        }else{
            $list=Brand::where([['status','=',1]])->order('create_time','desc')
              ->paginate(Config::get('app.admin_page_number'),false,['query' => $req]);
        }
        $data['list']=$list; 
        if(empty($req['brand_name'])){$data['brand_name']='';}else{$data['brand_name']=$req['brand_name'];}
        if(empty($req['isindex'])){$data['isindex']='';}else{$data['isindex']=$req['isindex'];}
        return $data;
	}
	/*新增*/
	public function insert_data($bname,$img,$url,$isindex){
        $Brand = new Brand;
		$result=$Brand->save([
		    'bname'  =>  trim($bname),
		    'img' =>  trim($img),
		    'linkurl' =>  trim($url),
		    'isindex' =>  trim($isindex),
		    'create_time' => date("Y-m-d H:i:s",time())
		]);
		return $result;
	}
	/*修改*/
	public function update_data($id,$bname,$img,$url,$isindex){
        $data = Brand::get($id);
        $data->bname = $bname;
        $data->img = $img;
        $data->linkurl = $url;
        $data->isindex = $isindex;
        $result=$data->save();
		return $result;    
	}
	/*删除假动作实际为修改操作*/
	public function delete_data($id){
        $data = Brand::get($id);
        if($data['status']!=1){
        	$result=['status'=>'0','msg'=>'无需操作'];
	        return json($result);
        }
		$data->status = '2';
		$result=$data->save();
		return $result;
	}
}
 